/**
 *
 * MergeExistingHouseholds.java
 * This class enables the user to merge households
 * when two families are married, or two families
 * existing in the parish are joined.
 *
 */

package gui;

import db.DBManager;
import struct.*;
import javax.swing.*;
import javax.swing.table.*;
import java.util.*;
import java.util.ArrayList;
import java.util.Arrays;

/**
 *
 * @author Sarah Wegner
 */
public class MergeExistingHouseholds extends javax.swing.JFrame {

    /**
     * Variables
     */
    ArrayList<String> familyList;
    String[] secondaryFamArray = new String[2];
    String[] primaryFamArray = new String[2];

    /** Creates new form MergeExistingHouseholds */
    public MergeExistingHouseholds() {
        initComponents();
        initFields();
    }//Constructor

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        cbPrimaryHouse = new javax.swing.JComboBox();
        lblTitle = new javax.swing.JLabel();
        jSeparator4 = new javax.swing.JSeparator();
        lblPrimaryHouse = new javax.swing.JLabel();
        lblMergeHouse = new javax.swing.JLabel();
        cbMergeHouse = new javax.swing.JComboBox();
        btMerge = new javax.swing.JButton();
        btCancel = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        txtFamilyName1 = new javax.swing.JTextField();
        txtPrimaryPhone1 = new javax.swing.JTextField();
        txtHeadOfHouse1 = new javax.swing.JTextField();
        txtSalutation1 = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        txtFirstName1 = new javax.swing.JTextField();
        txtLastName1 = new javax.swing.JTextField();
        txtDOB1 = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        txtDOB2 = new javax.swing.JTextField();
        txtPrimaryPhone2 = new javax.swing.JTextField();
        txtHeadOfHouse2 = new javax.swing.JTextField();
        txtFamilyName2 = new javax.swing.JTextField();
        txtLastName2 = new javax.swing.JTextField();
        txtFirstName2 = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        txtNotes1 = new javax.swing.JTextField();
        jSeparator5 = new javax.swing.JSeparator();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        jLabel12 = new javax.swing.JLabel();
        jLabel13 = new javax.swing.JLabel();
        jLabel14 = new javax.swing.JLabel();
        jLabel15 = new javax.swing.JLabel();
        jLabel16 = new javax.swing.JLabel();
        jLabel17 = new javax.swing.JLabel();
        jLabel18 = new javax.swing.JLabel();
        jLabel19 = new javax.swing.JLabel();
        jLabel20 = new javax.swing.JLabel();
        txtSalutation2 = new javax.swing.JTextField();
        txtNotes2 = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        cbPrimaryHouse.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                cbPrimaryHouseItemStateChanged(evt);
            }
        });

        lblTitle.setFont(new java.awt.Font("Tahoma", 0, 16)); // NOI18N
        lblTitle.setText("Merge Existing Households");

        jSeparator4.setForeground(new java.awt.Color(0, 153, 0));

        lblPrimaryHouse.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        lblPrimaryHouse.setText("Select Primary Household:");

        lblMergeHouse.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        lblMergeHouse.setText("Select Household to Merge:");

        cbMergeHouse.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                cbMergeHouseItemStateChanged(evt);
            }
        });

        btMerge.setText("Merge");
        btMerge.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btMergeActionPerformed(evt);
            }
        });

        btCancel.setText("Cancel");
        btCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btCancelActionPerformed(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel1.setForeground(new java.awt.Color(102, 102, 102));
        jLabel1.setText("Family Information");

        txtFamilyName1.setEditable(false);
        txtFamilyName1.setEnabled(false);

        txtPrimaryPhone1.setEditable(false);
        txtPrimaryPhone1.setEnabled(false);

        txtHeadOfHouse1.setEditable(false);
        txtHeadOfHouse1.setEnabled(false);

        txtSalutation1.setEditable(false);
        txtSalutation1.setEnabled(false);

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel2.setForeground(new java.awt.Color(102, 102, 102));
        jLabel2.setText("Adult Information");

        txtFirstName1.setEditable(false);
        txtFirstName1.setEnabled(false);

        txtLastName1.setEditable(false);
        txtLastName1.setEnabled(false);

        txtDOB1.setEditable(false);
        txtDOB1.setEnabled(false);

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel3.setForeground(new java.awt.Color(102, 102, 102));
        jLabel3.setText("Adult Information");

        txtDOB2.setEditable(false);
        txtDOB2.setEnabled(false);

        txtPrimaryPhone2.setEditable(false);
        txtPrimaryPhone2.setEnabled(false);

        txtHeadOfHouse2.setEditable(false);
        txtHeadOfHouse2.setEnabled(false);

        txtFamilyName2.setEditable(false);
        txtFamilyName2.setEnabled(false);

        txtLastName2.setEditable(false);
        txtLastName2.setEnabled(false);

        txtFirstName2.setEditable(false);
        txtFirstName2.setEnabled(false);

        jLabel4.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel4.setForeground(new java.awt.Color(102, 102, 102));
        jLabel4.setText("Family Information");

        txtNotes1.setEditable(false);
        txtNotes1.setEnabled(false);

        jSeparator5.setForeground(new java.awt.Color(0, 153, 0));

        jLabel5.setFont(new java.awt.Font("Tahoma", 2, 11)); // NOI18N
        jLabel5.setForeground(new java.awt.Color(153, 153, 153));
        jLabel5.setText("Family Name");

        jLabel6.setFont(new java.awt.Font("Tahoma", 2, 11)); // NOI18N
        jLabel6.setForeground(new java.awt.Color(153, 153, 153));
        jLabel6.setText("Primary Phone");

        jLabel7.setFont(new java.awt.Font("Tahoma", 2, 11)); // NOI18N
        jLabel7.setForeground(new java.awt.Color(153, 153, 153));
        jLabel7.setText("Head Of House");

        jLabel8.setFont(new java.awt.Font("Tahoma", 2, 11)); // NOI18N
        jLabel8.setForeground(new java.awt.Color(153, 153, 153));
        jLabel8.setText("Salutation");

        jLabel9.setFont(new java.awt.Font("Tahoma", 2, 11)); // NOI18N
        jLabel9.setForeground(new java.awt.Color(153, 153, 153));
        jLabel9.setText("Notes");

        jLabel10.setFont(new java.awt.Font("Tahoma", 2, 11)); // NOI18N
        jLabel10.setForeground(new java.awt.Color(153, 153, 153));
        jLabel10.setText("Notes");

        jLabel11.setFont(new java.awt.Font("Tahoma", 2, 11)); // NOI18N
        jLabel11.setForeground(new java.awt.Color(153, 153, 153));
        jLabel11.setText("Salutation");

        jLabel12.setFont(new java.awt.Font("Tahoma", 2, 11)); // NOI18N
        jLabel12.setForeground(new java.awt.Color(153, 153, 153));
        jLabel12.setText("Head Of House");

        jLabel13.setFont(new java.awt.Font("Tahoma", 2, 11)); // NOI18N
        jLabel13.setForeground(new java.awt.Color(153, 153, 153));
        jLabel13.setText("Primary Phone");

        jLabel14.setFont(new java.awt.Font("Tahoma", 2, 11)); // NOI18N
        jLabel14.setForeground(new java.awt.Color(153, 153, 153));
        jLabel14.setText("Family Name");

        jLabel15.setFont(new java.awt.Font("Tahoma", 2, 11)); // NOI18N
        jLabel15.setForeground(new java.awt.Color(153, 153, 153));
        jLabel15.setText("First Name");

        jLabel16.setFont(new java.awt.Font("Tahoma", 2, 11)); // NOI18N
        jLabel16.setForeground(new java.awt.Color(153, 153, 153));
        jLabel16.setText("Last Name");

        jLabel17.setFont(new java.awt.Font("Tahoma", 2, 11)); // NOI18N
        jLabel17.setForeground(new java.awt.Color(153, 153, 153));
        jLabel17.setText("Date Of Birth");

        jLabel18.setFont(new java.awt.Font("Tahoma", 2, 11)); // NOI18N
        jLabel18.setForeground(new java.awt.Color(153, 153, 153));
        jLabel18.setText("First Name");

        jLabel19.setFont(new java.awt.Font("Tahoma", 2, 11)); // NOI18N
        jLabel19.setForeground(new java.awt.Color(153, 153, 153));
        jLabel19.setText("Last Name");

        jLabel20.setFont(new java.awt.Font("Tahoma", 2, 11)); // NOI18N
        jLabel20.setForeground(new java.awt.Color(153, 153, 153));
        jLabel20.setText("Date Of Birth");

        txtSalutation2.setEditable(false);
        txtSalutation2.setEnabled(false);

        txtNotes2.setEditable(false);
        txtNotes2.setEnabled(false);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 144, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(lblTitle, javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jSeparator4, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 510, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(lblPrimaryHouse, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(cbPrimaryHouse, javax.swing.GroupLayout.PREFERRED_SIZE, 421, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addGroup(layout.createSequentialGroup()
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                            .addComponent(jLabel5)
                                            .addComponent(txtFamilyName1, javax.swing.GroupLayout.PREFERRED_SIZE, 191, javax.swing.GroupLayout.PREFERRED_SIZE))
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                            .addComponent(txtPrimaryPhone1, javax.swing.GroupLayout.PREFERRED_SIZE, 191, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addComponent(jLabel6))
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                            .addComponent(txtHeadOfHouse1, javax.swing.GroupLayout.PREFERRED_SIZE, 191, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addComponent(jLabel7, javax.swing.GroupLayout.PREFERRED_SIZE, 77, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addComponent(txtDOB1, javax.swing.GroupLayout.DEFAULT_SIZE, 191, Short.MAX_VALUE)
                                            .addComponent(jLabel20))))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(txtSalutation1, javax.swing.GroupLayout.PREFERRED_SIZE, 191, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jLabel8))))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(txtNotes1, javax.swing.GroupLayout.PREFERRED_SIZE, 360, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel9))
                        .addGap(35, 35, 35))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 144, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 142, Short.MAX_VALUE)
                                        .addComponent(jLabel18))
                                    .addComponent(txtFirstName1, javax.swing.GroupLayout.DEFAULT_SIZE, 193, Short.MAX_VALUE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel19, javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(txtLastName1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 189, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(168, 168, 168)))
                        .addGap(627, 627, 627))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jSeparator5, javax.swing.GroupLayout.PREFERRED_SIZE, 511, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addGroup(layout.createSequentialGroup()
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                                .addComponent(jLabel14)
                                                .addComponent(txtFamilyName2, javax.swing.GroupLayout.PREFERRED_SIZE, 191, javax.swing.GroupLayout.PREFERRED_SIZE))
                                            .addComponent(lblMergeHouse, javax.swing.GroupLayout.DEFAULT_SIZE, 193, Short.MAX_VALUE))
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addGroup(layout.createSequentialGroup()
                                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                                    .addComponent(txtPrimaryPhone2, javax.swing.GroupLayout.PREFERRED_SIZE, 191, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                    .addComponent(jLabel13))
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                                    .addComponent(txtHeadOfHouse2, javax.swing.GroupLayout.PREFERRED_SIZE, 191, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                    .addComponent(jLabel12))
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                                    .addComponent(txtSalutation2, javax.swing.GroupLayout.PREFERRED_SIZE, 191, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                    .addComponent(jLabel11))
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                                    .addGroup(layout.createSequentialGroup()
                                                        .addComponent(btCancel)
                                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                        .addComponent(btMerge))
                                                    .addComponent(txtNotes2, javax.swing.GroupLayout.DEFAULT_SIZE, 360, Short.MAX_VALUE)
                                                    .addComponent(jLabel10)))
                                            .addComponent(cbMergeHouse, javax.swing.GroupLayout.PREFERRED_SIZE, 388, javax.swing.GroupLayout.PREFERRED_SIZE))))
                                .addGap(35, 35, 35))
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 144, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 566, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addGroup(layout.createSequentialGroup()
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addGroup(layout.createSequentialGroup()
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 181, Short.MAX_VALUE)
                                                .addComponent(jLabel15))
                                            .addComponent(txtFirstName2, javax.swing.GroupLayout.DEFAULT_SIZE, 232, Short.MAX_VALUE))
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                            .addComponent(txtLastName2, javax.swing.GroupLayout.DEFAULT_SIZE, 233, Short.MAX_VALUE)
                                            .addComponent(jLabel16))
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                            .addComponent(txtDOB2, javax.swing.GroupLayout.DEFAULT_SIZE, 233, Short.MAX_VALUE)
                                            .addComponent(jLabel17))))
                                .addGap(473, 473, 473))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 144, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 1004, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addContainerGap())))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lblTitle)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator4, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(17, 17, 17)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblPrimaryHouse)
                    .addComponent(cbPrimaryHouse, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtFamilyName1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtPrimaryPhone1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtHeadOfHouse1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtSalutation1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtNotes1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(2, 2, 2)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(jLabel6)
                    .addComponent(jLabel7)
                    .addComponent(jLabel8)
                    .addComponent(jLabel9))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtFirstName1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtDOB1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtLastName1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(2, 2, 2)
                        .addComponent(jLabel18))
                    .addGroup(layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel19)
                            .addComponent(jLabel20))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 17, Short.MAX_VALUE)
                .addComponent(jSeparator5, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblMergeHouse)
                    .addComponent(cbMergeHouse, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(23, 23, 23)
                .addComponent(jLabel4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtFamilyName2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtPrimaryPhone2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtHeadOfHouse2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtSalutation2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtNotes2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel14)
                    .addComponent(jLabel13)
                    .addComponent(jLabel12)
                    .addComponent(jLabel11)
                    .addComponent(jLabel10))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtFirstName2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtLastName2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtDOB2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(btMerge)
                        .addComponent(btCancel))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel15)
                        .addComponent(jLabel16)
                        .addComponent(jLabel17)))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btCancelActionPerformed
        this.dispose();
    }//GEN-LAST:event_btCancelActionPerformed

    private void btMergeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btMergeActionPerformed
        // Verify required information is filled before an actual merge in
        // the db are committed.
        String selectionsNeeded = verifySelections();
        boolean diffSelections = verifyDiffSelections();

            //if selectionsNeeded is not empty, then selections were not made.
            if(!selectionsNeeded.equalsIgnoreCase("")){
                JOptionPane.showMessageDialog(null, "Please make a selection from: "
                        + selectionsNeeded, "Merge Households", JOptionPane.WARNING_MESSAGE);
            }//if
            else if(!diffSelections)
            {
                JOptionPane.showMessageDialog(null, "Please select different"
                        + " households for the Primary and Merging fields.",
                        "Merge Households", JOptionPane.WARNING_MESSAGE);
            }//if
            else{
//                //Both Selections have been made
//                //Either family cannot have more than one adult.
                //Get Primary Family Information
                String primaryFamily = cbPrimaryHouse.getSelectedItem().toString();
                primaryFamArray = primaryFamily.split(";");

                String primaryFamName = primaryFamArray[0];
                String primaryPhone = primaryFamArray[1];
//                int numPrimaryAdults = 0;
//
//              //Get Secondary Family Information
                String secondaryFamily = cbMergeHouse.getSelectedItem().toString();
                secondaryFamArray = secondaryFamily.split(";");

                String secondaryFamName = secondaryFamArray[0];
                String secondaryPhone = secondaryFamArray[1];

                //All conditions have been met ok to merge
                int answer = JOptionPane.showConfirmDialog(null, "Are you sure "+
                        "you would like to merge these families?" + "\n" + "\n"
                        + "Please note, contributions and family ministry information "
                 + "\n" + "(both interests and histories) will be permanently removed "
                 + "\n" + "from the Household to Merge and will not be carried over " + "\n"
                        + "to the resulting family if  you continue.",
                        "Merge Households", JOptionPane.YES_NO_OPTION);
                if(answer == JOptionPane.YES_OPTION){
                    merge();
                }//if
                //else, do nothing
            }//else
    }//GEN-LAST:event_btMergeActionPerformed

    private void cbPrimaryHouseItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_cbPrimaryHouseItemStateChanged
        //Fill the text boxes with information on select of a new item
        try{
            DBManager dbManager = new DBManager();

            //Get primary household attributes
            String familyKey = cbPrimaryHouse.getSelectedItem().toString();
            if(!familyKey.equals("")){
                String[] familyArray = familyKey.split(";");
                Family family = dbManager.getFamily(familyArray[0], familyArray[1]);
                Object[] familyAttributes = family.getAttributes();

                //set the text fields to the family attributes
                if(familyAttributes[0] != null){
                    txtFamilyName1.setText(familyAttributes[0].toString());
                }//if
                if(familyAttributes[1] != null){
                    txtPrimaryPhone1.setText(familyAttributes[1].toString());
                }//if
                if(familyAttributes[2] != null){
                    txtHeadOfHouse1.setText((familyAttributes[2].toString()));
                }//if
                if(familyAttributes[5] != null){
                    txtSalutation1.setText(familyAttributes[5].toString());
                }//if
                if(familyAttributes[6] != null){
                    txtNotes1.setText(familyAttributes[6].toString());
                }//if

                //get primary household adult information
                ArrayList<Adult> adultList = dbManager.getAdults(familyArray[0], familyArray[1]);
                if(!adultList.isEmpty()){
                    if(adultList.get(0)!= null){
                        if(adultList.get(0).getFirstName() != null){
                            txtFirstName1.setText(adultList.get(0).getFirstName());
                        }//if
                        if(adultList.get(0).getLastName() != null){
                            txtLastName1.setText(adultList.get(0).getLastName());
                        }//if
                        if(adultList.get(0).getDateOfBirth() != null){
                            txtDOB1.setText(adultList.get(0).getDateOfBirth());
                        }//if

                    }//if
                }//if
                else{
                    txtFirstName1.setText("");
                    txtLastName1.setText("");
                    txtDOB1.setText("");
                }//else
            }//if
            else{
                //Clear text boxes, there is no family selection
                txtFamilyName1.setText("");
                txtPrimaryPhone1.setText("");
                txtHeadOfHouse1.setText("");
                txtSalutation1.setText("");
                txtNotes1.setText("");
                txtFirstName1.setText("");
                txtLastName1.setText("");
                txtDOB1.setText("");
            }//else
        }//try
        catch(Exception ex){
            ex.printStackTrace();
            JOptionPane.showMessageDialog(null, "Sorry, there was an error with the application." + "\n" +
                "Please contact your Database Administrator, a.k.a Thomas Naps, for help if this persists."
                + "\n" + "MergeExistingHouseholds.java: cbPrimaryHouseholdItemStateChanged() Method.",
                "Application Error", JOptionPane.ERROR_MESSAGE);
        }//catch
    }//GEN-LAST:event_cbPrimaryHouseItemStateChanged

    private void cbMergeHouseItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_cbMergeHouseItemStateChanged
        //Fill the text boxes with information on select of a new item
        try{
            DBManager dbManager = new DBManager();

            //Get primary household attributes
            String familyKey = cbMergeHouse.getSelectedItem().toString();
            if(!familyKey.equals("")){
                String[] familyArray = familyKey.split(";");
                Family family = dbManager.getFamily(familyArray[0], familyArray[1]);
                Object[] familyAttributes = family.getAttributes();

                //set the text fields to the family attributes
                if(familyAttributes[0] != null){
                    txtFamilyName2.setText(familyAttributes[0].toString());
                }//if
                if(familyAttributes[1] != null){
                    txtPrimaryPhone2.setText(familyAttributes[1].toString());
                }//if
                if(familyAttributes[2] != null){
                    txtHeadOfHouse2.setText((familyAttributes[2].toString()));
                }//if
                if(familyAttributes[5] != null){
                    txtSalutation2.setText(familyAttributes[5].toString());
                }//if
                if(familyAttributes[6] != null){
                    txtNotes2.setText(familyAttributes[6].toString());
                }//if

                //get primary household adult information
                ArrayList<Adult> adultList = dbManager.getAdults(familyArray[0], familyArray[1]);
                if(!adultList.isEmpty()){
                    if(adultList.get(0)!= null){
                        if(adultList.get(0).getFirstName() != null){
                            txtFirstName2.setText(adultList.get(0).getFirstName());
                        }//if
                        if(adultList.get(0).getLastName() != null){
                            txtLastName2.setText(adultList.get(0).getLastName());
                        }//if
                        if(adultList.get(0).getDateOfBirth() != null){
                            txtDOB2.setText(adultList.get(0).getDateOfBirth());
                        }//if

                    }//if
                }//if
                else{
                    txtFirstName2.setText("");
                    txtLastName2.setText("");
                    txtDOB2.setText("");
                }//else
            }//if
            else{
                //set each text box to empty because the family selection is empty.
                txtFamilyName2.setText("");
                txtPrimaryPhone2.setText("");
                txtHeadOfHouse2.setText("");
                txtSalutation2.setText("");
                txtNotes2.setText("");
                txtFirstName2.setText("");
                txtLastName2.setText("");
                txtDOB2.setText("");
            }//else
        }//try
        catch(Exception ex){
            ex.printStackTrace();
            JOptionPane.showMessageDialog(null, "Sorry, there was an error with the application." + "\n" +
                "Please contact your Database Administrator, a.k.a Thomas Naps, for help if this persists."
                + "\n" + "MergeExistingHouseholds.java: cbMergeHouseholdItemStateChanged() Method.",
                "Application Error", JOptionPane.ERROR_MESSAGE);
        }//catch
    }//GEN-LAST:event_cbMergeHouseItemStateChanged

    /**
     * Opens and runs the gui
     * @param args the command line arguments
     */
    public static void run() {
        new MergeExistingHouseholds().setVisible(true);
    }//run


    /**
     * A method to initialize the drop-downs.
     *
     */
    private void initFields(){
        try{
            DBManager dbManager = new DBManager();
            familyList = dbManager.getAllFamilyKeys();

            //populate primary household drop-down and set selected item to ""
            cbPrimaryHouse.addItem("");
            cbMergeHouse.addItem("");
            for(int i = 0; i < familyList.size(); i++){
                primaryFamArray = familyList.get(i).split(";");
                String primaryFamName = primaryFamArray[0];
                String primaryPhone = primaryFamArray[1];
                int numPrimaryAdults = dbManager.numberOfAdults(primaryFamName, primaryPhone);
                if(numPrimaryAdults == 1){
                    cbPrimaryHouse.addItem(familyList.get(i));
                    cbMergeHouse.addItem(familyList.get(i));
                }//if
            }//for
            cbPrimaryHouse.setSelectedItem("");
            cbMergeHouse.setSelectedItem("");

        }//try
        catch(Exception ex){
            
        }//catch
    }//initFields

    /**
     * Merge households in the db
     */
    private void merge(){
        try{
            DBManager dbManager = new DBManager();
            boolean merged = dbManager.mergeHouseholds(primaryFamArray, secondaryFamArray);
            if(!merged){
                JOptionPane.showMessageDialog(null, "Sorry, the merge was not successful." + "\n" +
                "Please contact your Database Administrator, a.k.a Thomas Naps, for help if this persists."
                + "\n" + "MergeExistingHouseholds.java: merge() Method.",
                "Database Saving Error", JOptionPane.ERROR_MESSAGE);
            }//if
            else{
                //Merge was successful
                JOptionPane.showMessageDialog(null, "You have "
                        + "successfully merged these households.",
                        "Merge Households",
                        JOptionPane.INFORMATION_MESSAGE);
                this.dispose();
            }//else
        }//try
        catch(Exception ex){
            JOptionPane.showMessageDialog(null, "Sorry, the merge cannot continue due to an error." + "\n" +
                "Please contact your Database Administrator, a.k.a Thomas Naps, for help if this persists."
                + "\n" + "MergeExistingHouseholds.java: merge() Method.",
                "Database Saving Error", JOptionPane.ERROR_MESSAGE);
        }//catch
    }//merge

    /**
     * Method to be sure that the selected drop-downs
     * are different.
     * @return boolean = true if they are different,
     * false if they are the same.
     */
    private boolean verifyDiffSelections(){
        String primary = cbPrimaryHouse.getSelectedItem().toString();
        String secondary = cbMergeHouse.getSelectedItem().toString();

        if(primary.equalsIgnoreCase(secondary)){
            return false;
        }
        return true;
    }

    /**
     * A method to verify each drop-down, primary household and merging
     * household has been selected
     * @return String - a string that contains which selections need to be made.
     * Merge Household, Primary Household, or "".
     */
    private String verifySelections(){
        // Verify required information is filled before an actual merge in
        // the db are committed.
        boolean primaryHouseEmpty = false;
        boolean mergeHouseEmpty = false;
        String selectionsNeeded = "";

        if(cbPrimaryHouse.getSelectedItem().toString().equalsIgnoreCase("")){
            primaryHouseEmpty = true;
        }//if
        if(cbMergeHouse.getSelectedItem().toString().equalsIgnoreCase("")){
            mergeHouseEmpty = true;
        }//if

        //selections NOT made for primary or merge households
        if(primaryHouseEmpty || mergeHouseEmpty){
            if(primaryHouseEmpty){
                selectionsNeeded = "Primary Household";
            }//if
            if(mergeHouseEmpty){
                selectionsNeeded = "\n" + "Household to Merge";
            }//if
        }//if
        return selectionsNeeded;
    }//verifySelctions

    private boolean oneAdult(String famName, String famPhone){
        return true;
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btCancel;
    private javax.swing.JButton btMerge;
    private javax.swing.JComboBox cbMergeHouse;
    private javax.swing.JComboBox cbPrimaryHouse;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel15;
    private javax.swing.JLabel jLabel16;
    private javax.swing.JLabel jLabel17;
    private javax.swing.JLabel jLabel18;
    private javax.swing.JLabel jLabel19;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel20;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JSeparator jSeparator4;
    private javax.swing.JSeparator jSeparator5;
    private javax.swing.JLabel lblMergeHouse;
    private javax.swing.JLabel lblPrimaryHouse;
    private javax.swing.JLabel lblTitle;
    private javax.swing.JTextField txtDOB1;
    private javax.swing.JTextField txtDOB2;
    private javax.swing.JTextField txtFamilyName1;
    private javax.swing.JTextField txtFamilyName2;
    private javax.swing.JTextField txtFirstName1;
    private javax.swing.JTextField txtFirstName2;
    private javax.swing.JTextField txtHeadOfHouse1;
    private javax.swing.JTextField txtHeadOfHouse2;
    private javax.swing.JTextField txtLastName1;
    private javax.swing.JTextField txtLastName2;
    private javax.swing.JTextField txtNotes1;
    private javax.swing.JTextField txtNotes2;
    private javax.swing.JTextField txtPrimaryPhone1;
    private javax.swing.JTextField txtPrimaryPhone2;
    private javax.swing.JTextField txtSalutation1;
    private javax.swing.JTextField txtSalutation2;
    // End of variables declaration//GEN-END:variables

}
